package com.yytek.nmp.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.yytek.common.core.utils.DateUtils;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.security.utils.SecurityUtils;
import com.yytek.nmp.domain.IotDeviceGroup;
import com.yytek.nmp.mapper.IotDeviceGroupMapper;
import com.yytek.nmp.mapper.IotProjectUserTopoMapper;
import com.yytek.nmp.service.IIotDeviceInfoService;
import com.yytek.nmp.vo.ProjectBindUserVo;
import com.yytek.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.nmp.mapper.IotProjectInfoMapper;
import com.yytek.nmp.domain.IotProjectInfo;
import com.yytek.nmp.service.IIotProjectInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Service
public class IotProjectInfoServiceImpl implements IIotProjectInfoService 
{
    @Autowired
    private IotProjectInfoMapper iotProjectInfoMapper;
    @Autowired
    private IIotDeviceInfoService iotDeviceInfoService;
    @Autowired
    private IotProjectUserTopoMapper iotProjectUserTopoMapper;
    @Autowired
    private IotDeviceGroupMapper iotDeviceGroupMapper;
    /**
     * 查询项目
     * 
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public IotProjectInfo selectIotProjectInfoById(Long id)
    {
        return iotProjectInfoMapper.selectIotProjectInfoById(id);
    }

    /**
     * 查询项目列表
     * 
     * @param iotProjectInfo 项目
     * @return 项目
     */
    @Override
    public List<IotProjectInfo> selectIotProjectInfoList(IotProjectInfo iotProjectInfo)
    {
        return iotProjectInfoMapper.selectIotProjectInfoList(iotProjectInfo);
    }

    /**
     * 新增项目
     * 
     * @param iotProjectInfo 项目
     * @return 结果
     */
    @Override
    public AjaxResult insertIotProjectInfo(IotProjectInfo iotProjectInfo)
    {
        //校验全局项目名称重复
        List<IotProjectInfo> selectProjectInfo = iotProjectInfoMapper.selectProjectByName(iotProjectInfo.getProjectName());
        if(selectProjectInfo != null && selectProjectInfo.size() >0){
            return AjaxResult.error("项目名称重复,请用其他名称");
        }else{
            //设置启用
            iotProjectInfo.setStatus(1);
            iotProjectInfo.setCreateBy(SecurityUtils.getUserId().toString());
            iotProjectInfo.setCreateTime(DateUtils.getNowDate());
            int i = iotProjectInfoMapper.insertIotProjectInfo(iotProjectInfo);
            return i > 0 ? AjaxResult.success() : AjaxResult.error("新增失败");
        }

    }

    /**
     * 修改项目
     * 
     * @param iotProjectInfo 项目
     * @return 结果
     */
    @Override
    public int updateIotProjectInfo(IotProjectInfo iotProjectInfo)
    {
        iotProjectInfo.setUpdateTime(DateUtils.getNowDate());
        return iotProjectInfoMapper.updateIotProjectInfo(iotProjectInfo);
    }

    /**
     * 批量删除项目
     * 
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteIotProjectInfoByIds(Long[] ids)
    {
        boolean isCanDelete = false;
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            if(iotDeviceInfoService.isExistsDeviceByProjectId(id)){
                return AjaxResult.error("项目下绑定设备,请删除设备再删除项目");
            }else{
                isCanDelete = true;
            }
        }
        //如果都可以删除
        if(isCanDelete){
            //删除 项目授权关系,项目分组,项目
            iotProjectInfoMapper.deleteIotProjectInfoByIds(ids);
            iotProjectUserTopoMapper.deleteIotProjectUserTopoByProjectIds(ids);
            iotDeviceGroupMapper.deleteIotDeviceGroupByProjectIds(ids);
        }

        return AjaxResult.success();
    }

    /**
     * 删除项目信息
     * 
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteIotProjectInfoById(Long id)
    {
        return iotProjectInfoMapper.deleteIotProjectInfoById(id);
    }

    /**
     * 查询授权项目列表
     * @param userId 用户id
     * @return
     */
    @Override
    public List<IotProjectInfo> selectAuthProjectInfoListByUserId(String userId) {
        return iotProjectInfoMapper.selectAuthProjectInfoListByUserId(userId);
    }

    /**
     * 查询该项目下所有授权人员
     * @param id 项目id
     * @return
     */
    @Override
    public List<SysUser> getAuthUserListByProjectId(String id) {
        return iotProjectInfoMapper.getAuthUserListByProjectId(id);
    }


    /**
     * 绑定项目的授权人员
     * @param projectBindUserVo
     * @return
     */
    @Override
    @Transactional
    public int authProjectBindUsers(ProjectBindUserVo projectBindUserVo) {
        return iotProjectUserTopoMapper.insertAuthProjectBindUsers(projectBindUserVo);
    }

    /**
     * 是否存在自己项目
     * @param iotProjectInfo
     * @return
     */
    @Override
    public boolean existMyProjectByName(IotProjectInfo iotProjectInfo) {
        String userId = SecurityUtils.getUserId().toString();
        String name = iotProjectInfo.getProjectName();
        //先查询是否新增或编辑
        //如果存在id是编辑
        if(iotProjectInfo.getId() != null){
            //先查找自己名称,如果相同返回false
            IotProjectInfo cx = selectIotProjectInfoById(iotProjectInfo.getId());
            if(cx.getProjectName().equals(iotProjectInfo.getProjectName())){
                return false;
            }else{
                //如果不同,是修改了名称,则查询是否重复
                List<IotProjectInfo> reList = iotProjectInfoMapper.selectProjectByUserIdAndName(userId,name);
                if(CollectionUtil.isNotEmpty(reList)){
                    return true;
                }else {
                    return false;
                }
            }
        }else{
            //不存在是新增
            List<IotProjectInfo> reList = iotProjectInfoMapper.selectProjectByUserIdAndName(userId,name);
            if(CollectionUtil.isNotEmpty(reList)){
                return true;
            }else {
                return false;
            }
        }
    }
}
